package org.mili.ticketeer.dto;

import org.mili.application.dto.Archivable;
import org.mili.application.dto.Field;
import org.mili.application.dto.Identifieable;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

public class Ticket extends Identifieable implements Archivable {

    public static enum Fields implements Field {
        GROUP,
        STATE,
        PRIORITY,
        CREATEDAT,
        COMPLETEDAT,
        TITLE,
        COMMENT;
    }

    public static enum UniqueFields implements Field {
        GROUP,
        TITLE;
    }

    public static final String TABLE = "ticket";

    private int group;
    private int state;
    private int priority;
    private Timestamp createdAt;
    private Timestamp completedAt;
    private String title;
    private String comment;

    @Override
    public Map<Field, Object> valueMap() {
        Map<Field, Object> map = super.valueMap();
        map.put(Fields.GROUP, group);
        map.put(Fields.STATE, state);
        map.put(Fields.PRIORITY, priority);
        map.put(Fields.CREATEDAT, createdAt);
        map.put(Fields.COMPLETEDAT, completedAt);
        map.put(Fields.TITLE, title);
        map.put(Fields.COMMENT, comment);
        return map;
    }

    @Override
    public Map<Field, Object> valueMapWithoutId() {
        Map<Field, Object> map = new HashMap<>(valueMap());
        map.remove(Identifieable.Fields.ID);
        return map;
    }

    @Override
    public Map<Field, Object> getUniqueFields() {
        Map<Field, Object> map = new HashMap<>();
        map.put(Fields.GROUP, group);
        map.put(Fields.TITLE, title);
        return map;
    }

    public int getGroup() {
        return group;
    }

    public void setGroup(int group) {
        this.group = group;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public Timestamp getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Timestamp createdAt) {
        this.createdAt = createdAt;
    }

    public Timestamp getCompletedAt() {
        return completedAt;
    }

    public void setCompletedAt(Timestamp completedAt) {
        this.completedAt = completedAt;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Ticket ticket = (Ticket) o;

        if (group != ticket.group) return false;
        if (priority != ticket.priority) return false;
        if (state != ticket.state) return false;
        if (comment != null ? !comment.equals(ticket.comment) : ticket.comment != null) return false;
        if (completedAt != null ? !completedAt.equals(ticket.completedAt) : ticket.completedAt != null) return false;
        if (createdAt != null ? !createdAt.equals(ticket.createdAt) : ticket.createdAt != null) return false;
        if (title != null ? !title.equals(ticket.title) : ticket.title != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = group;
        result = 31 * result + state;
        result = 31 * result + priority;
        result = 31 * result + (createdAt != null ? createdAt.hashCode() : 0);
        result = 31 * result + (completedAt != null ? completedAt.hashCode() : 0);
        result = 31 * result + (title != null ? title.hashCode() : 0);
        result = 31 * result + (comment != null ? comment.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Ticket{" +
                "group=" + group +
                ", state=" + state +
                ", priority=" + priority +
                ", createdAt=" + createdAt +
                ", completedAt=" + completedAt +
                ", title='" + title + '\'' +
                ", comment='" + comment + '\'' +
                '}';
    }
}
